//import angular core
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';

//import third package
import 'rxjs/add/operator/switchMap';

//import service
import { RoomService } from '../../service/room.service';

//import class
import { Room } from '../../domain/room';
declare var layui: any;
@Component({
  selector: 'app-room-detail',
  templateUrl: './room-detail.component.html',
  styleUrls: ['./room-detail.component.css'],
  providers: [RoomService]
})
export class RoomDetailComponent implements OnInit {

  private room: Room;
  private roomUpdate: Room;

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private roomService: RoomService,
    private location: Location
  ) { }

  ngOnInit() {

    this.route.params
      .switchMap((params: Params) => this.roomService.getRoomById(+params['id']))
      .subscribe(res => {
        this.room = res;
        this.roomUpdate = res;
      });
  }

  updateRoom(): void {
    this.roomService.updateRoom(this.roomUpdate)
    .then(()=>{
      this.goBack();
    })
  }

  reset(): void {
    this.roomUpdate = this.room;
  }

  goBack(): void {
    this.location.back();
  }
}
